﻿// Sequin.Sequences.SequenceFormatter
//
// (c) 2011 Arthur Pitman
//
// Part of the Sequin Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License (Version 2.1) as published by the Free Software Foundation.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License (Version 2.1) along with this library; if not, write to the 
// Free Software Foundation, Inc., 
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
// or see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sequin.Sequences
{
	/// <summary>
	/// 
	/// </summary>
	public class SequenceFormatter : ISequenceFormatter
	{
		/// <summary>
		/// Splitter to split a sequence into elements
		/// </summary>
		protected static char[] sequenceSplitter = { ';' };

		/// <summary>
		/// Splitter to split an element into items
		/// </summary>
		protected static char[] elementSplitter = { ',' };

		/// <summary>
		/// Converts an element to a <see cref="string"/>
		/// </summary>
		/// <param name="element">The element to convert</param>
		/// <param name="symbolMapper">The <see cref="ISymbolMapper"/> to use</param>
		/// <returns>A <see cref="string"/> representation of the element</returns>
		public string ElementToString(int[] element, ISymbolMapper symbolMapper)
		{
			StringBuilder elementBuilder = new StringBuilder();
			bool firstItem = true;
			foreach (var item in element)
			{
				if (!firstItem)
					elementBuilder.Append(",");
				elementBuilder.Append(symbolMapper.IdToSymbol(item));
				firstItem = false;
			}
			return elementBuilder.ToString();
		}

		/// <summary>
		/// Converts a <see cref="Sequence"/> to a <see cref="string"/>
		/// </summary>
		/// <param name="sequence">The <see cref="Sequence"/> to convert</param>
		/// <param name="symbolMapper">The <see cref="ISymbolMapper"/> to use</param>
		/// <returns>A <see cref="string"/></returns>
		public string SequenceToString(Sequence sequence, ISymbolMapper symbolMapper)
		{
			StringBuilder sequenceBuilder = new StringBuilder();
			bool firstElement = true;
			foreach (var element in sequence.Value)
			{
				if (!firstElement)
					sequenceBuilder.Append(";");
				sequenceBuilder.Append(ElementToString(element, symbolMapper));
				firstElement = false;
			}
			return sequenceBuilder.ToString();
		}

		/// <summary>
		/// Converts a <see cref="string"/> to an element
		/// </summary>
		/// <param name="elementString">The <see cref="string"/> to convert</param>
		/// <param name="symbolMapper">The <see cref="ISymbolMapper"/> to use</param>
		/// <returns>An element</returns>
		public int[] StringToElement(string elementString, ISymbolMapper symbolMapper)
		{
			string[] items = elementString.Split(elementSplitter, StringSplitOptions.RemoveEmptyEntries);
			List<int> element = new List<int>();
			foreach (var s in items)
			{
				int itemId = symbolMapper.SymbolToId(s.Trim());
				if (!element.Contains(itemId))
					element.Add(itemId);
			}
			element.Sort();
			return element.ToArray();
		}

		/// <summary>
		/// Converts a <see cref="string"/> to a <see cref="Sequence"/>
		/// </summary>
		/// <param name="sequenceString">The <see cref="string"/> to convert</param>
		/// <param name="symbolMapper">The <see cref="ISymbolMapper"/> to use</param>
		/// <returns>A <see cref="Sequence"/></returns>
		public Sequence StringToSequence(string sequenceString, ISymbolMapper symbolMapper)
		{
			List<int[]> sequence = new List<int[]>();
			string[] elements = sequenceString.Split(sequenceSplitter, StringSplitOptions.RemoveEmptyEntries);
			foreach (var elementString in elements)
			{
				sequence.Add(StringToElement(elementString, symbolMapper));
			}
			return new Sequence(sequence.ToArray());
		}
	}
}
